package com.netcracker.studportal.server.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.netcracker.studportal.server.dao.UniversityDAO;
import com.netcracker.studportal.server.model.University;
import com.netcracker.studportal.server.util.HibernateUtil;

public class UniversityDAOImpl implements UniversityDAO {
	private static final Logger LOG = Logger.getLogger(UniversityDAOImpl.class);

	@Override
	public void Add(University univ) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.save(univ);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public void Update(University univ) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.update(univ);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public University getById(Long id) throws Exception {
		Session session = null;
		University univ = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			univ = (University) session.load(University.class, id);
			//TODO what is there initializes?
			Hibernate.initialize(univ);
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return univ;
	}

	@Override
	public void Delete(University univ) throws Exception {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(univ);
			session.getTransaction().commit();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	@Override
	public Collection<University> getAll() throws Exception {
		Session session = null;
		List<University> universities = new ArrayList<University>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			universities = session.createCriteria(University.class).list();
		} catch (Exception e) {
			LOG.error("Error: ", e);
			// TODO delete it, show errors in log
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return universities;
	}
	
	/**
	 * Realization of singleton.
	 */
	private UniversityDAOImpl() {}
	private static UniversityDAO instance;
	public static UniversityDAO getInstance() {
		if (instance == null) {
			instance = new UniversityDAOImpl();
		}
		return instance;
	}

}
